<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-08-15
 * Time: 20:56
 */
namespace app\wxapi\controller;

use app\common\controller\Wxapibase;
use app\common\model\OrderPlay;
use app\common\model\OrderShop;
use app\common\model\UserMoney;
use weixinhack\WxBase;

class Wxpay extends Wxapibase {

    public function index(){
        $getdata = request()->param();
        if(empty($getdata['oid'])){
            $this->error("违法访问");
        }
        //  查询订单信息
        $order = model('Order')->where('oid',$getdata['oid'])->find()->toArray();
        $order['conf'] = unserialize($order['conf']);
        $order['loid'] = $this->linshioid($order['oid']);
        if($order['mid'] == '100'){
            $order['title'] = $this->webdb['webname']."购物订单".$order['oid'];
        }
        //  下单
        $wxmodel = new WxBase();
        $repaly = $wxmodel->pay_add($this->webdb,$order);
        $wxjsapi = $wxmodel->get_jsapi($this->wxtoken);
        if($wxjsapi == false){
            $this->error("唤起微信支付失败，请重新提交订单！");
        }
        $wxjsapi = $wxmodel->get_jssing($wxjsapi,url('index',array('oid'=>$order['oid'])));
        if($repaly['return_code'] == 'SUCCESS' && $repaly['return_msg'] == 'OK' && $repaly['result_code'] == 'SUCCESS'){
            //  调用支付
            $repaly = $wxmodel->get_play($this->webdb,$repaly);
            $this->assign('repaly',$repaly);
        }else{
            $this->error('唤起微信支付失败，请重新提交订单！');
        }
        $this->assign('order',$order);
        $this->assign('wxjsapi',$wxjsapi);
        return view();
    }

    //  生成临时订单号并存入数据库
    public function linshioid($oid){
        $numbers = range (1,50);
        shuffle ($numbers);
        $num=8;
        $result = array_slice($numbers,0,$num);
        $result = implode('',$result);
        $newname = 'LSPAY'.date("Ymd");
        for ( $i = 0; $i < 8; $i++ ) {
            $newname .= substr($result, mt_rand(0, strlen($result) - 1), 1);
        }
        $data['oid'] = $oid;
        $data['lsoid'] = $newname;
        model('OrderPlay')->save($data);
        return $newname;
    }
    //  支付完成处理
    public function endplay(){
        $loid = request()->param('loid');
        //  查询订单
        $wxmodel = new WxBase();
        $playmodel = new OrderPlay();
        $gomodel = new OrderShop();
        $ormodel = new \app\common\model\Order();
        $endplay = $wxmodel->get_payend($this->webdb,$loid);
        if($endplay['return_code'] == 'SUCCESS' && $endplay['result_code'] == 'SUCCESS' && $endplay['trade_state'] == 'SUCCESS'){
            //  修改支付单号
            $orderplay = $playmodel->where('lsoid',$loid)->find()->toArray();
            $orderplay['status'] = $endplay['transaction_id'];
            $playmodel->allowField(true)->isUpdate(true)->save($orderplay,['lsoid'=>$loid]);
            $playmodel->where('oid',$orderplay['oid'])->where('status','0')->delete();
            //  修改支付状态
            $order = $ormodel->where('oid',$orderplay['oid'])->find()->toArray();
            $order['playtime'] = time();
            $order['playstatus'] = '1';
            $ormodel->allowField(true)->isUpdate(true)->save($order,['oid'=>$order['oid']]);
            //  清空购物车
            if($order['mid'] == '100'){
                $orderShop = $gomodel->where('uid',$this->cxbsuser['uid'])->find()->toArray();
                $orderShop['aids'] = '';
                $gomodel->allowField(true)->isUpdate(true)->save($orderShop,['id'=>$orderShop['id']]);
                //  处理产品数量
                $shop = unserialize($order['conf']);
                $pudata = null;
                foreach ($shop['shop'] as $k => $v){
                    $donshop = null;
                    $donshop = db('article_content_100')->where('aid',$v['aid'])->find();

                    $donshop['kucun'] = $donshop['kucun'] - $v['num'];
                    $pudata[$v['aid']]['aid'] = $donshop['aid'];
                    $pudata[$v['aid']]['fengmoney'] = $donshop['fengmoney'] * $v['num'] * 100;
                    db('article_content_100')->where('aid',$donshop['aid'])->setField('kucun',$donshop['kucun']);
                }
                $puid = model('UserSpread')->where('uid',$this->cxbsuser['uid'])->count();
                if($puid > 0 && $this->cxbsuser['group_id'] != '8' && !empty($pudata)){
                    $puser = model('UserSpread')->where('uid',$this->cxbsuser['uid'])->find()->toArray();
                    $monty = array(
                        'uid' => $puser['puid'],
                        'addcont' => '3',
                    );
                    $moneymodel = new UserMoney();
                    foreach ($pudata as $v){
                        $usermoney = null;
                        $usermoney = model('UserMoney')->adduser($monty,$v['fengmoney']);
                        $usermoney['cont'] = $puser['username'].'购买：'.model('Article')->where('aid',$v['aid'])->value('title').'，增加：'.$v['fengmoney'].'元';
                        $usermoney['fuid'] = $this->cxbsuser['uid'];
                        $usermoney['uid'] = $monty['uid'];
                        $usermoney['addtime'] = time();
                        $moneymodel->setAttr('id',null)->isUpdate(false)->save($usermoney);
                    }
                }
            }
            $this->assign('endplay',$endplay);
            return view();
        }
        $this->error("非法访问！");
    }
}